package com.glodon.paas.framework.registry.core.zk;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

import java.util.List;

/**
 * Watch the session state changes, execute the callbacks when the registered state happends
 *
 * @author Don Li
 */
public class SessionStateWatcher implements Watcher {

    private Event.KeeperState keeperState = Event.KeeperState.Disconnected;

    private SessionStateProcessor processor;

    private List<Event.KeeperState> registeredState;

    public SessionStateWatcher() {
    }

    public SessionStateWatcher(List<Event.KeeperState> registeredState, SessionStateProcessor processor) {
        this.registeredState = registeredState;
        this.processor = processor;
    }

    @Override
    public void process(WatchedEvent event) {
        this.keeperState = event.getState();
        if (registeredState != null && registeredState.contains(event.getState())) {
            processor.process(event);
        }
    }

    public void setProcessor(SessionStateProcessor processor) {
        this.processor = processor;
    }

    public void setRegisteredState(List<Event.KeeperState> registeredState) {
        this.registeredState = registeredState;
    }

    public Event.KeeperState getKeeperState() {
        return keeperState;
    }
}
